//
//  YHPUserAccount.swift
//  SinaSwift
//
//  Created by Yan on 16/3/10.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class YHPWelcomeViewController: UIViewController {
    
    override func loadView() {
        //大小会自动设置
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    //设置UI
    private func setupUI() {
        //添加子视图
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        iconImageView.sd_setImageWithURL(YHPUserAccount.shareAccount.headImageURL)
       
        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        iconImageView.layer.borderWidth = 5
        iconImageView.layer.borderColor = UIColor.purpleColor().CGColor
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(iconImageView.snp_centerX)
        }
    }
    
    //界面已经出现的时候会自动调用
    //这个方法是页面显示之后的生命周期方法 在这个方法中所有的子视图的位置 已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    func startAnimation () {
        //1.在动画闭包中 更新约束  snp_updateConstraints: 如果该约束 已经存在 就更新约束 ,如果不存在就创建约束
        //usingSpringWithDamping: 弹簧系数  0 ~ 1  越小越弹
        //initialSpringVelocity: 加速度 9.8  重力加速度  如果希望弹簧动画效果比较好 一般设置范围 弹簧系数 * 10 ~= 加速度
        //options: 动画的可选项  枚举  OC:  位移枚举  '|'  , swift中 使用数组表示多个枚举选项
        
        welcomeLabel.alpha = 0
       let offSetY = 180 - UIScreen.mainScreen().bounds.height
        UIView.animateWithDuration(1.5, delay: 0.1, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offSetY)
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in

                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion:{ (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                })
        }

    }
    
    //MARK: 懒加载子视图
    private lazy var backImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
//    private lazy var  welcomeLabel : UILabel = labelWith((YHPUserAccount.shareAccount.name ?? "") + "  欢迎归来", textColor: UIColor.darkGrayColor(), fontSize:(16))
    private lazy var welcomeLabel : UILabel = UIFactory.factoryLabelWith((YHPUserAccount.shareAccount.name ?? "") + "  欢迎归来", textColor: UIColor.darkGrayColor(), fontSize: (16))
}
